import 'package:flutter/material.dart';

abstract class BackLayerItem extends StatefulWidget {
  const BackLayerItem({Key? key, required this.index}) : super(key: key);

  final int index;
}

class BackLayer extends StatefulWidget {
  const BackLayer(
      {Key? key, required this.backLayerItems, required this.tabController})
      : super(key: key);

  final List<BackLayerItem> backLayerItems;
  final TabController tabController;

  @override
  _BackLayerState createState() => _BackLayerState();
}

class _BackLayerState extends State<BackLayer> {
  @override
  void initState() {
    super.initState();
    widget.tabController.addListener(() => setState(() {}));
  }

  @override
  Widget build(BuildContext context) {
    final tabIndex = widget.tabController.index;
    return IndexedStack(
      index: tabIndex,
      children: [
        for (BackLayerItem backLayerItem in widget.backLayerItems)
          ExcludeFocus(
            child: backLayerItem,
            excluding: backLayerItem.index != tabIndex,
          ),
      ],
    );
  }
}
